package me.luochenyxh.base.base


import android.os.Bundle
import android.view.View
import androidx.annotation.LayoutRes
import androidx.appcompat.app.AppCompatActivity
import com.blankj.utilcode.util.ToastUtils
import com.gyf.immersionbar.ImmersionBar
import me.hgj.jetpackmvvm.network.manager.NetState
import me.hgj.jetpackmvvm.network.manager.NetworkStateManager
import me.hgj.jetpackmvvm.util.LogUtils
import me.luochenyxh.base.R
import me.luochenyxh.base.base.status.AppStatusManager
import me.luochenyxh.base.util.YBaseActivityHelper


/**
 * @FileName: YBaseActivity
 * @author: yuan_xh
 * @date: 2022-5-13
 * @version V1.0
 * @desc Activity的基类
 */
abstract class YBaseActivity : AppCompatActivity() {
    private var TAG: String = javaClass.simpleName

    /**
     * 是否占位状态栏，默认为True
     */
    open fun settingFullScreen(): Boolean = true

    /**
     * 设置系统状态栏颜色
     */
    open fun settingStatusBarColor(): Int = R.color.transparent

    /**
     * 设置状态栏颜色，默认黑色
     */
    open fun settingStatusDarkFont(): Boolean = true


    abstract fun getContentView(): View

    /**
     * 所有初始化工作完成后调用该方法
     */
    abstract fun onCreated()

    override fun onCreate(savedInstanceState: Bundle?) {
        checkAppStatus()
        super.onCreate(savedInstanceState)
        YBaseActivityHelper(this).initAc()
        statusBaySetting()
        setContentView(getContentView())

        onCreated()

        NetworkStateManager.instance.mNetworkStateCallback.observeSticky(this) {
            onNetworkStateChanged(it)
        }
    }


    /**長時間置于后台回来后*/
    open fun checkAppStatus() {
        //判断app状态
        if (AppStatusManager.isRecycle()) {
            LogUtils.d("BaseActivity-${TAG}","$TAG = 被回收")
            ToastUtils.showShort("App被回收了")
            return
        }
    }

    /**
     * activity系统状态栏封装，并设置默认样式
     */
    private fun statusBaySetting() {
        ImmersionBar.with(this)
                .statusBarColor(settingStatusBarColor())       //状态栏颜色，不写默认透明色
                .statusBarDarkFont(settingStatusDarkFont())        //状态栏字体是深色，不写默认为亮色
                .fitsSystemWindows(!settingFullScreen())     //解决状态栏和布局重叠问题，任选其一，默认为false，当为true时一定要指定statusBarColor()，不然状态栏为透明色，还有一些重载方法
                .init()
    }

    /**
     * 网络变化监听 子类重写
     */
    open fun onNetworkStateChanged(netState: NetState) {}


}